Compiling Monads ∗
نویسنده
چکیده
Computational monads offer a powerful way to parameterize functional specifications, but they give rise to exceedingly tedious simplifications to instantiate this “monadic” interpreter. We report on the use of partial evaluation to achieve the following instantiations automatically. • We derive equivalent formulations of the monadic λ-interpreter, based on equivalent specifications of monads from algebra and category theory (join, clone, exp, and bind). This increases flexibility in formulating the parameterized interpreter. • We derive several well-known styled interpreters by instantiating the monadic interpreter with a particular monad (store, continuation, etc.). This illustrates the generality of the monadic interpreter. • We specialize the monadic interpreter with respect to a particular program, thereby compiling this program into a parameterized representation of its meaning (a.k.a. a monadic form). This makes it possible to reason about particular programs independently of their interpretation and also to measure the generality of the monadic interpreter. From a computational viewpoint, partial evaluation makes it possible for each of these instantiations to run significantly faster. In the spirit of the Scheme programming language, our Kleisli interpreter handles a fixed set of special forms. Therefore, the natural way to extend it is to add predefined functions in the initial environment, using Church encoding (call/cc instead of escape, etc.) if necessary. We study the consequences of this design. Equipping the exception monad with the usual operators forces us to adopt a new representation of arrows and therefore to modify the interpreter in a way reminiscent of call-by-name, even though the defined language is still call-by-value. Instantiating this new interpreter with the continuation monad yields the lesser-known continuation-passing style advocated by Reynolds. Finally, we relate the parameterization offered by monads with other ways of structuring a formal semantics, either for the purpose of flow analysis by abstract interpretation or for the purpose of semantics-directed compiling and compiler generation.
منابع مشابه
Leibniz’s Monads and Mulla Sadra’s Hierarchy of Being: A Comparative Study
Mulla Sadra and Leibniz, the two philosophers from the East and the West, belong to two different worlds. Though they were unaware of the ideas of each other, their philosophical systems share certain common points that are comparable. Monads constitute the basis of Leibniz's thought and he refers to their features in his various works. On the other side, Mulla Sadra's philosophy is also based ...
متن کاملSwinging Types At Work
We present a number of swinging specifications with visible and/or hidden components, such as lists, sets, bags, maps, monads, streams, trees, graphs, processes, nets, classes, languages, parsers,... They provide more or less worked-out case studies and shall allow the reader to figure out the integrative power of the swinging type approach with respect to various specification and proof formal...
متن کاملMonads Need Not Be Endofunctors
We introduce a generalisation of monads, called relative monads, allowing for underlying functors between different categories. Examples include finite-dimensional vector spaces, untyped and typed λ-calculus syntax and indexed containers. We show that the Kleisli and Eilenberg-Moore constructions carry over to relative monads and are related to relative adjunctions. Under reasonable assumptions...
متن کاملComplete Elgot Monads and Coalgebraic Resumptions
Monads are extensively used nowadays to abstractly model a wide range of computational effects such as nondeterminism, statefulness, and exceptions. It turns out that equipping a monad with a (uniform) iteration operator satisfying a set of natural axioms allows for modelling iterative computations just as abstractly. The emerging monads are called complete Elgot monads. It has been shown recen...
متن کاملCombining Monads
Monads provide a way of structuring functional programs. Most real applications require a combination of primitive monads. Here we describe how some monads may be combined with others to yield a combined monad.
متن کامل